#include <bits/stdc++.h>
using namespace std;

int n, m, dfn[20001], low[20001], flag[20001], dep[20001], idx;
vector<int> edge[20001];
string ans = "1";

string mul(string s, int m)
{
    if (s == "0" || m == 0)
        return "0";

    string r;
    long long c = 0;

    for (int i = s.length() - 1; i >= 0; --i) {
        long long p = (long long)(s[i] - '0') * m + c;
        r.push_back((p % 10) + '0');
        c = p / 10;
    }

    while (c) {
        r.push_back((c % 10) + '0');
        c /= 10;
    }

    reverse(r.begin(), r.end());
    return r;
}

void tarjan(int u, int fa)
{
    dfn[u] = low[u] = ++idx, dep[u] = dep[fa] + 1;
    int cnt = 0;
    for (int x : edge[u]) {
        if (!dfn[x]) {
            tarjan(x, u);
            low[u] = min(low[u], low[x]);
            if (low[x] < dfn[u])
                cnt++;
        } else if (x != fa) {
            low[u] = min(low[u], dfn[x]);
            if (dfn[x] < dfn[u])
                cnt++, ans = mul(ans, (dep[u] > dep[x] + 1 ? (dep[u] - dep[x] + 2) : 1));
        }
    }
    if (cnt > 1) {
        cout << 0;
        exit(0);
    }
}

signed main()
{
    cin >> n >> m;
    for (int i = 1; i <= m; i++) {
        int k, x, y;
        cin >> k;
        cin >> x;
        while (--k) {
            cin >> y;
            edge[x].push_back(y);
            edge[y].push_back(x);
            x = y;
        }
    }
    tarjan(1, 1);
    if (idx != n)
        cout << 0;
    else
        cout << ans;
    return 0;
}